//
//  FFWeChatHandler.m
//  Components
//
//  Created by IT.Bear on 16/7/7.
//  Copyright (c) 2016年 FashionFinger. All rights reserved.
//


#import "FFWeChatHandler.h"
#import "WXApi.h"
#import "JSONKit.h"
//#import "FFAlertView.h"

@interface FFWeChatHandler ()

@property (nonatomic, strong) FFPayInfo *payInfo;

@end

@implementation FFWeChatHandler

- (id)initWithPayInfo:(FFPayInfo *)payInfo {
    self = [super init];
    if (self) {
        _payInfo = payInfo;
    }
    return self;
}

- (void)goPay {
    if ([WXApi isWXAppInstalled]) {
        NSDictionary * dic = (NSDictionary *)_payInfo.info;
        if (dic) {
            PayReq *request = [[PayReq alloc] init];
            request.partnerId = dic[@"partnerid"];
            request.prepayId= dic[@"prepayid"];
            request.package = @"Sign=WXPay";
            request.nonceStr= dic[@"noncestr"];
            request.timeStamp= ((NSNumber *)dic[@"timestamp"]).intValue;
            request.sign= dic[@"sign"];
    
            [WXApi sendReq:request];
        }else{
            if (self.delegate && [self.delegate respondsToSelector:@selector(weChatHandler:payResult:)]) {
                [self.delegate weChatHandler:self payResult:FFPayResultWeChatPayFailed];
            }
        }
    }else{
        UIAlertController *alerController = [UIAlertController alertControllerWithTitle:nil message:@"还没有安装微信，是否下载安装？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"点击了取消");
            if (self.delegate && [self.delegate respondsToSelector:@selector(weChatHandler:payResult:)]) {
                [self.delegate weChatHandler:self payResult:FFPayResultWeChatPayFailed];
            }
        }];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"点击了确定");
            if (self.delegate && [self.delegate respondsToSelector:@selector(weChatHandler:payResult:)]) {
                [self.delegate weChatHandler:self payResult:FFPayResultWeChatPayFailed];
            }
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/wei-xin/id414478124?mt=8"]];
            [alerController dismissViewControllerAnimated:YES completion:nil];
        }];
        [alerController addAction:action];
        [alerController addAction:action1];
        
        [[self getCurrentVC] presentViewController:alerController animated:YES completion:nil];
    }
}

//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}

@end
